package com.kejiguan.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.kejiguan.ui.adapter.NeItemAdapter;
import com.kejiguan.ui.dialog.AddNeItemDialog;
import com.kejiguan.ui.dialog.AddNeItemDialogUtil;
import com.kejiguan.ui.dialog.CommDialogUtil;
import com.kejiguan.ui.dialog.ModifyNeItemDialog;
import com.kejiguan.ui.dialog.ModifyNeItemDialogUtil;
import com.kejiguan.ui.model.NeItem;
import com.kejiguan.ui.presenter.NeItemManagerPresenter;
import com.kejiguan.ui.response.NeItemManagerResponse;
import com.kejiguan.ui.response.QueryAllNeItemResponse;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * Created by YYY on 16/7/18.
 */
public class NeIitemManagerFragment extends Fragment implements View.OnClickListener,View.OnLongClickListener,AddNeItemDialog.OnNewNeItemAddListener,ModifyNeItemDialog.OnNeItemModifiedListener{


    private View rootView;
    private RecyclerView neItemList;
    private View addBtn;

    private NeItemManagerPresenter mPresenter;
    private NeItemAdapter neItemAdapter;
    private NeItem modifyNeItem,delNeItem;

    @Override
    public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        mPresenter=new NeItemManagerPresenter();
        mPresenter.attach(getContext());
        EventBus.getDefault().register(this);
    }

    @Override
    public final View onCreateView(LayoutInflater inflater, ViewGroup container,
                                   Bundle savedInstanceState) {
        if (null==rootView)
        {
            rootView=LayoutInflater.from(getContext()).inflate(R.layout.fragment_neitem_manager_layout,null);
            neItemList= (RecyclerView) rootView.findViewById(R.id.neitem__list);
            addBtn=rootView.findViewById(R.id.add_neitem_btn);
            addBtn.setOnClickListener(this);
            addBtn.setVisibility(View.GONE);

            LinearLayoutManager layoutManager=new LinearLayoutManager(getContext());
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            neItemList.setLayoutManager(layoutManager);

            neItemAdapter=new NeItemAdapter(getContext(),null);
            neItemAdapter.setOnClickListener(NeIitemManagerFragment.this);
            neItemAdapter.setOnLongClickListener(NeIitemManagerFragment.this);
            neItemList.setAdapter(neItemAdapter);


        }
        return rootView;
    }

    @Override
    public void onResume()
    {
        super.onResume();
        mPresenter.queryNeItems();
    }

    public void reLoad()
    {
        if (null!=mPresenter)
        {
            mPresenter.queryNeItems();
        }
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(QueryAllNeItemResponse response)
    {

        if ("0000".equals(response.getRetCode())&&null!=neItemAdapter)
        {
            neItemAdapter.setNeItems(response.getEmptyNeItem());
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(NeItemManagerResponse response) {
        if ("0000".equals(response.getRetCode())) {

            String operType=response.getOperType();
            switch (operType)
            {
                case "add":
                    neItemAdapter.addNeItem(response.getNeItem());
                    break;
                case "delete":
                    neItemAdapter.removeNeItem(response.getNeItem());
                    break;
                case "mod":
                    neItemAdapter.modifyNeItem(response.getNeItem());
                    break;
            }
            mPresenter.queryNeItems();
        } else {

            String operType=response.getOperType();
            switch (operType)
            {
                case "add":
                    Toast.makeText(getContext(),response.getRetInfo(),Toast.LENGTH_SHORT).show();
                    break;
                case "delete":
                    Toast.makeText(getContext(),response.getRetInfo(),Toast.LENGTH_SHORT).show();
                    break;
                case "mod":
                    Toast.makeText(getContext(),response.getRetInfo(),Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.add_neitem_btn:
                AddNeItemDialogUtil.getInstance().show(getContext(),NeIitemManagerFragment.this);
                break;
            case R.id.ne_item:
                int position= (int) v.getTag(R.id.recyclerview_item_position);
                modifyNeItem=neItemAdapter.getNeItems().get(position);
                ModifyNeItemDialogUtil.getInstance().show(getContext(),modifyNeItem,NeIitemManagerFragment.this);
                break;
            case R.id.dialog_confirm_btn:
                CommDialogUtil.getInstance().dismiss();
                mPresenter.delNeItem(delNeItem);
                break;
            case R.id.dialog_cancel_btn:
                CommDialogUtil.getInstance().dismiss();
                break;
        }

    }

    @Override
    public void onNewNeItemAdd(NeItem neItem) {
        mPresenter.addNeItem(neItem);
    }

    @Override
    public void neItemModifiedListener(NeItem item) {
        mPresenter.modifyNeItem(item);
    }

    @Override
    public boolean onLongClick(View v) {
        switch (v.getId())
        {
            case R.id.ne_item:
                int position= (int) v.getTag(R.id.recyclerview_item_position);
                delNeItem=neItemAdapter.getNeItems().get(position);
                CommDialogUtil.getInstance().show(getContext(),"要删除"+ delNeItem.getNeName()+"展项吗?",NeIitemManagerFragment.this,"删除","取消",0);
                break;
        }
        return false;
    }
}
